# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:mybro01.py
@Time:2022/2/24 16:04

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import time
import math
import os
from csvdictrw01 import csvdictrw
from csv2mysql import Mysql_csv

from PIL import Image
from PIL import ImageGrab

from cjy import *
from listhebing import *

class Mybro:
    def __init__(self,Uname,Pwd):
        self.sql=Mysql_csv()
        flag = 1
        while flag:
            try:
                time_start = time.time()

                opt = Options()
                opt.add_experimental_option('excludeSwitches', ['enable-automation'])  # 关闭自动化提示
                opt.add_argument("--start-maximized")  # 最大化
                opt.add_argument('ignore-certificate-errors')  # 加上不出现私密连接
                self.bro = webdriver.Chrome(r'C:\Users\hsk\Downloads\chromedriver_win32/chromedriver.exe', options=opt)

                self.bro.get('https://10.6.133.106/ydata/login.do')
                self.bro.implicitly_wait(10)

                username = self.bro.find_element_by_name("userId")
                password = self.bro.find_element_by_name("passwd")
                vdcode = self.bro.find_element_by_name("imgtvalidate")
                vdcode_img = self.bro.find_element_by_xpath('//*[@id="img"]')

                # 用户名
                username.send_keys(Uname)
                # 密码
                password.send_keys(Pwd)
                # 验证码图片
                location = vdcode_img.location

                size = vdcode_img.size
                print(location, size)
                box = (
                location['x'], location['y'] + 70, location['x'] + size['width'], location['y'] + 70 + size['height'])
                print(box)

                self.bro.save_screenshot('c:\\users\\hsk\desktop\\vdcode_3.png')
                img = Image.open('c:\\users\\hsk\desktop\\vdcode_3.png')
                vdcode_path = 'c:\\users\\hsk\\desktop\\vdcode_4.png'

                frame = ImageGrab.grab(box)

                frame.save(vdcode_path)

                chaojiying = Chaojiying_Client('zgq001', 'zgq071726', '924951')
                im = open(vdcode_path, 'rb').read()
                result = chaojiying.PostPic(im, 1004)['pic_str']
                print(result)

                vdcode.send_keys(result)

                login = self.bro.find_element_by_xpath('/html/body/map/area[1]')

                login.click()
                for handle in self.bro.window_handles:
                    # 先切换到该窗口
                    self.bro.switch_to.window(handle)
                    # print(handle)
                    # 得到该窗口的标题栏字符串，判断是不是我们要操作的那个窗口
                    # '必应'可，'Bing'可？
                    if '系统错误' in self.bro.title:
                        # 如果是，那么这时候WebDriver对象就是对应的该窗口，正好，跳出循环，
                        break

                # wd.title属性是当前窗口的标题栏文本
                # print(bro.title)
                # bro.find_element(By.XPATH,'//*[@id="aback"]/img').click()
                # a=3/0
                # //*[@id="aback"]/img
                if self.bro.title == '系统错误':
                    self.bro.quit()
                    flag = 1
                else:
                    flag = 0

            except Exception as e:
                print(e)
                # bro.quit()
                # flag=1
        self.switchquery()

    def closewindow(self):
        #self.bro.close()
        self.bro.quit()

    def switchquery(self):
        self.bro.switch_to.frame('olp_leftFrame')
        self.bro.find_element_by_xpath('//*[@id="item6"]').click()
        self.bro.find_element_by_xpath('//*[@id="M04"]/tbody/tr[1]/td[2]/a').click()
        self.bro.switch_to.default_content()

    def getzdopts(self):
        # 右侧框架
        self.bro.switch_to.frame('olp_mainFrame')
        # bro.find_element_by_xpath('//*[@id="tables"]/tbody/tr[21]/td[3]/a').click()
        # bro.find_element(By.ID,'periodType')

        # 选择制度,得到制度列表
        zdlist = []
        zdxz = self.bro.find_element(By.ID, 'regime')
        options_zdlist = zdxz.find_elements(By.TAG_NAME, 'option')
        for option in options_zdlist:
            zdlist.append(option.text)
        #print(zdlist)
        # 退出框架
        self.bro.switch_to.default_content()
        return zdlist

    def getyjnopts(self,zd):
        # 右侧框架
        self.bro.switch_to.frame('olp_mainFrame')
        # 创建Select对象
        select = Select(self.bro.find_element(By.ID, "regime"))

        # 通过 Select 对象选中月报
        select.select_by_visible_text(zd)
        #print(select.select_by_visible_text(zd))
        #print(select.options)
        # 选择月季年,得到月季年列表
        yjnlist = []
        yjnxz = self.bro.find_element(By.ID, 'periodType')
        #print("&"*30)
        #print(yjnxz.text)
        options_yjnlist = yjnxz.find_elements(By.TAG_NAME, 'option')
        for option in options_yjnlist:
            yjnlist.append(option.text)
        # moren=self.bro.find_element(By.CSS_SELECTOR,'#periodType>option')
        # print(f'moren:{moren.text}')
        # moren=self.bro.find_element(By.XPATH,'//*[@id="periodType"]')
        # print(f'moren:{moren.text}')
        #print(yjnlist)
        # 退出框架
        self.bro.switch_to.default_content()
        return yjnlist

    def getbblist(self,zd,zq):
        # 右侧框架
        self.bro.switch_to.frame('olp_mainFrame')
        # 创建Select对象
        select = Select(self.bro.find_element(By.ID, "regime"))
        # 通过 Select 对象选中月报,zd制度
        select.select_by_visible_text(zd)
        # 创建Select对象
        select1 = Select(self.bro.find_element(By.ID, "periodType"))
        # 通过 Select 对象选中月报,yjn月季年
        select1.select_by_visible_text(zq)
        time.sleep(1)
        # 报表选择,得到报表列表
        # table#tables td:nth-child(4)
        bblist = []
        bbxz = self.bro.find_elements(By.CSS_SELECTOR, 'table#tables td:nth-child(4)')
        #self.bro.implicitly_wait(10)

        for bb in bbxz:
            if bb.text!='':
                bblist.append(bb.text)
        print(bblist)
        # # 报表选择器列表
        # bbxzqlist = []
        # for i in range(1, len(bbxz) + 1):
        #     strgz = '//*[@id="tables"]/tbody/tr[' + str(i) + ']/td[4]/a'
        #     bbxzqlist.append(strgz)
        # #print(bbxzqlist)
        #
        # # 报表选择合并
        # bbxzhb = hblst2(bblist, bbxzqlist)
        # #print(bbxzhb)
        #
        # # 删除空项，列表中没有显示的项
        # bbxzhb1 = []
        # for i in range(len(bbxzhb)):
        #     if bbxzhb[i][0] != '':
        #         bbxzhb1.append(bbxzhb[i])
        #
        # #print(bbxzhb1)
        #
        # # 退出框架
        # self.bro.switch_to.default_content()
        # return (bbxzhb1)
        return bblist

    def getbblist1(self,zd,zq):
        # 右侧框架
        self.bro.switch_to.frame('olp_mainFrame')
        # 创建Select对象
        select = Select(self.bro.find_element(By.ID, "regime"))
        # 通过 Select 对象选中月报,zd制度
        select.select_by_visible_text(zd)
        # 创建Select对象
        select1 = Select(self.bro.find_element(By.ID, "periodType"))
        # 通过 Select 对象选中月报,yjn月季年
        select1.select_by_visible_text(zq)
        time.sleep(1)
        # 报表选择,得到报表列表
        # table#tables td:nth-child(4)
        bblist = []
        bbxz = self.bro.find_elements(By.CSS_SELECTOR, 'table#tables td:nth-child(4)')
        #self.bro.implicitly_wait(10)

        for bb in bbxz:
            #if bb.text!='':
            bblist.append(bb.text)
        #print(bblist)
        # 报表选择器列表
        bbxzqlist = []
        for i in range(1, len(bbxz) + 1):
            strgz = '//*[@id="tables"]/tbody/tr[' + str(i) + ']/td[4]/a'
            bbxzqlist.append(strgz)
        #print(bbxzqlist)

        # 报表选择合并
        bbxzhb = hblst2(bblist, bbxzqlist)
        #print(bbxzhb)

        # 删除空项，列表中没有显示的项
        bbxzhb1 = []
        for i in range(len(bbxzhb)):
            if bbxzhb[i][0] != '':
                bbxzhb1.append(bbxzhb[i])

        #print(bbxzhb1)

        # 退出框架
        self.bro.switch_to.default_content()

        return bblist,bbxzhb1

    #bb是XPATH
    def clickbb(self,bb):
        # 右侧框架
        self.bro.switch_to.frame('olp_mainFrame')
        self.bro.find_element(By.XPATH,bb).click()
        self.bro.switch_to.default_content()

    #得到查询目录列表
    def getquerylist(self):
        # 到‘基层数据查询’窗口
        for handle in self.bro.window_handles:
            # 先切换到该窗口
            self.bro.switch_to.window(handle)
            # print(handle)
            # 得到该窗口的标题栏字符串，判断是不是我们要操作的那个窗口
            # '必应'可，'Bing'可？
            if '基层数据查询' in self.bro.title:
                # 如果是，那么这时候WebDriver对象就是对应的该窗口，正好，跳出循环，
                break

        # wd.title属性是当前窗口的标题栏文本
        # print(bro.title)
        # 记录主窗口
        mainWindow = self.bro.current_window_handle
        self.bro.switch_to.frame('workFrame')

        # getselectoptions
        chaxunmb = self.bro.find_element(By.ID, 'querytemp')
        cxmblist = []
        options_list = chaxunmb.find_elements(By.TAG_NAME, 'option')
        for option in options_list:
            # print("value is:%s text is %s"%(option.get_attribute('value'),option.text))
            cxmblist.append(option.text)
        #print(cxmblist)
        self.bro.switch_to.default_content()
        return cxmblist

    def getbiaoti(self,query):
        #切换到工作框架
        self.bro.switch_to.frame('workFrame')
        #点击查询项目
        select = Select(self.bro.find_element(By.ID, "querytemp"))
        select.select_by_visible_text(query)
        # 得到标题行项目
        bt = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt>td')
        # for btx in bt:
        #     print(btx.text)

        # "写入文件"
        with open(r'data1.csv', 'w', encoding='utf-8') as f:
            for btx in bt:
                f.write(btx.text + ',')
            f.write('\n')
        # 读出修改
        with open(r'data1.csv', 'r', encoding='utf-8') as f:
            # str=f.read()
            # print(str)
            lt = f.readlines()
            #print(lt[0])
            #print(type(lt[0]))
            lt[0] = lt[0].replace(', ,', ',')
            lt[0] = lt[0].replace('操作,', '查看,往期,')
            lt[0] = lt[0].replace(r'[201-1] 行业代码(GB/T4754-2017)', '行业代码')
            lt[0] = lt[0].replace('期别(年)', '期别年')
            lt[0] = lt[0].replace('期别(月)', '期别月')
            lt[0] = lt[0].replace(r';', '')
            lt[0] = lt[0].replace(r'1-本月', '本期')
            # lt[0] = lt[0].replace('本期资产处置收益(损失以“-”号记)千元','本期资产处置收益千元')
            lt[0] = lt[0].replace('本期资产处置收益(损失以“-”号记)千元', '本期资产处置收益千元')
            lt[0] = lt[0].replace('上年同期资产处置收益(损失以“-”号记)千元', '上年同期资产处置收益千元')
            # lt[0] = lt[0].replace(r'1-本月;营业收入;千元', '本期营收')
            # lt[0] = lt[0].replace(r'上年同期;营业收入;千元', '上期营收')
            #print(lt[0])
            #print('#' * 30)
            #print(lt)
        # 再次写入修改好的标题行
        with open(r'data1.csv', 'w', encoding='utf-8') as f:
            f.writelines(lt)

        # 退出框架
        self.bro.switch_to.default_content()
        return lt

    def getdata(self):
        # 切换到工作框架
        self.bro.switch_to.frame('workFrame')
        # 记录主窗口
        mainWindow = self.bro.current_window_handle
        # 修改每页30行为100行
        # 占击每页30行/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[1]
        self.bro.find_element(By.XPATH, '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[1]').click()
        # 退出当前frame
        self.bro.switch_to.default_content()
        # 跳入新窗口&nbsp;
        for handle in self.bro.window_handles:
            # 先切换到该窗口
            self.bro.switch_to.window(handle)
            # print(handle)
            # 得到该窗口的标题栏字符串，判断是不是我们要操作的那个窗口
            # '必应'可，'Bing'可？
            if '&nbsp;' in self.bro.title:
                # 如果是，那么这时候WebDriver对象就是对应的该窗口，正好，跳出循环，
                break
        # 输入100//*[@id="pageSize"]
        self.bro.find_element(By.XPATH, '//*[@id="pageSize"]').send_keys('100')
        # 点击确定/html/body/div/span[1]/input
        #time.sleep(3)
        self.bro.find_element(By.XPATH, '/html/body/div/span[1]/input').click()
        # 回到主窗口
        self.bro.switch_to.window(mainWindow)
        # 回到workframe工作框架
        self.bro.switch_to.frame('workFrame')
        # ys 月数
        n = '2021'
        ys = [2]
        for y in ys:
            ny = n + '年' + str(y) + '月'
            select = Select(self.bro.find_element(By.NAME, "period"))

            # 通过 Select 对象选中
            select.select_by_visible_text(ny)
            time.sleep(1)
            # 30行数据
            datas = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt~tr')
            for data in datas:
                print(data.text)

            total_page =self.bro.find_element(By.ID, 'total_page')
            print(total_page.text)
            total_page = int(total_page.text[1:-1])
            # total_num=int(bro.find_element(By.ID,'total_num').text)
            # total_page=math.ceil(total_num/100)
            # print(total_page)
            page = 1
            for page in range(1, total_page + 1):
                # 退格符,两次,出现两次
                self.bro.find_element(By.XPATH,
                                 '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/input').send_keys('\b\b')
                self.bro.find_element(By.XPATH,
                                 '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/input').send_keys(page)
                self.bro.find_element(By.XPATH, '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[2]').click()
                trs = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt~tr')
                for tr in trs:
                    print(tr.text)

                with open(r'data1.csv', 'a', encoding='utf-8') as f:
                    for tr in trs:
                        tds = tr.find_elements(By.TAG_NAME, 'td')
                        for td in tds:
                            f.write(td.text + ',')
                        f.write('\n')
        # 退出框架
        self.bro.switch_to.default_content()
    #切换到---统计联网直报平台窗口
    def oldwindow(self):
        # 获取当前窗口句柄（窗口A）
        handle = self.bro.current_window_handle
        # 获取当前所有窗口句柄（窗口A、B）
        handles = self.bro.window_handles
        # print(handles)
        # 对窗口进行遍历
        for newhandle in handles:
            # 筛选新打开的窗口B
            if newhandle == handle:
                # 切换到新打开的窗口B
                self.bro.switch_to.window(newhandle)
                # 在新打开的窗口B中操作
                # browser.find_element_by_id('xx').click()
                # 关闭当前窗口B
                self.bro.close()
                # 切换回窗口A
                self.bro.switch_to.window(handles[0])
        #print('go to oldwindow')

    def getquery(self,zd,yjn):
        bblist=self.getbblist(zd,yjn)
        for bb in bblist:
            print(bb[0])
            self.clickbb(bb[1])
            # 得到查询列表
            cxmllist = self.getquerylist()
            # print(cxmllist)
            #cxmllist[0] = bb[0]
            print(cxmllist)
            self.oldwindow()

    def getquery1(self,bb):
        #bblist=self.getbblist(zd,yjn)
        #for bb in bbhb:
        print(bb[0])
        self.clickbb(bb[1])
        # 得到查询列表
        cxmllist = self.getquerylist()
        # print(cxmllist)
        #cxmllist[0] = bb[0]
        #print(cxmllist)
        self.oldwindow()
        return bb,cxmllist

    def find_key(self,input_dict, value):
        return list({k for k, v in input_dict.items() if v == value})[0]

    def getchaxunmulu(self,zd):
        with open('zdmulu.csv', 'r', encoding='u8') as f:
            zdmulu = f.read()
            zddict = eval(zdmulu)
        # zd = "企业一套表统计调查制度(2022)"
        # zd=zddict['23']
        # print(zd)
        zdcode = self.find_key(zddict, zd)
        print(zdcode)
        zqlist = self.getyjnopts(zd)# not self.bro.getyjnopts(zd)
        print(zqlist)
        with open(fr"chaxunmulu\{zd}.csv", 'w', encoding='u8') as f:
            # f.write(zd)
            f.write('')
        i = 1
        with open(fr"chaxunmulu\{zd}.csv", 'a', encoding='u8') as f:
            f.write('{')
            for zq in zqlist:
                j = 1
                bblist, bbhb = self.getbblist1(zd, zq)
                # print(bblist)
                print(bbhb)
                for bb in bbhb:
                    k = 1
                    bb, cxmllist = self.getquery1(bb)
                    # with open('mulu.csv', 'a', encoding='u8') as f:
                    #     f.write('{')
                    # print(f'制度：{zd},周期：{zq},报表：{bb},查询目录：{cxml}')
                    for cxml in cxmllist:
                        if j < 10:
                            if k < 10:
                                f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}0{j}':{bb},'{i}0{j}0{k}':'{cxml}'")
                                f.write(',\n')
                            else:
                                f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}0{j}':{bb},'{i}0{j}{k}':'{cxml}'")
                                f.write(',\n')
                        else:
                            if k < 10:
                                f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}{j}':{bb},'{i}{j}0{k}':'{cxml}'")
                                f.write(',\n')
                            else:
                                f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}{j}':{bb},'{i}{j}{k}':'{cxml}'")
                                f.write(',\n')
                        k += 1
                    j += 1
                i += 1
            f.write('}')
        self.closewindow()

    def paqudata(self,zd,zq,bbcode,cx):
        #self.switchquery()

        self.bro.switch_to.frame('olp_mainFrame')

        select = Select(self.bro.find_element(By.ID, "regime"))
        select.select_by_visible_text(zd)
        select1 = Select(self.bro.find_element(By.ID, "periodType"))
        select1.select_by_visible_text(zq)
        time.sleep(1)
        self.bro.find_element(By.XPATH, bbcode).click()

        self.bro.switch_to.default_content()

        for handle in self.bro.window_handles:
            # 先切换到该窗口
            self.bro.switch_to.window(handle)
            # print(handle)
            # 得到该窗口的标题栏字符串，判断是不是我们要操作的那个窗口
            # '必应'可，'Bing'可？
            if '基层数据查询' in self.bro.title:
                # 如果是，那么这时候WebDriver对象就是对应的该窗口，正好，跳出循环，
                break
        mainWindow = self.bro.current_window_handle
        self.bro.switch_to.frame('workFrame')
        select = Select(self.bro.find_element(By.ID, "querytemp"))
        select.select_by_visible_text(cx)



        # 占击每页30行/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[1]
        self.bro.find_element(By.XPATH, '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[1]').click()
        # 退出当前frame
        self.bro.switch_to.default_content()
        # 跳入新窗口&nbsp;
        for handle in self.bro.window_handles:
            # 先切换到该窗口
            self.bro.switch_to.window(handle)
            # print(handle)
            # 得到该窗口的标题栏字符串，判断是不是我们要操作的那个窗口
            # '必应'可，'Bing'可？
            if '&nbsp;' in self.bro.title:
                # 如果是，那么这时候WebDriver对象就是对应的该窗口，正好，跳出循环，
                break
        # 输入100//*[@id="pageSize"]
        self.bro.find_element(By.XPATH, '//*[@id="pageSize"]').send_keys('100')
        # 点击确定/html/body/div/span[1]/input
        # time.sleep(3)
        self.bro.find_element(By.XPATH, '/html/body/div/span[1]/input').click()
        # 回到主窗口
        self.bro.switch_to.window(mainWindow)
        # 回到workframe工作框架
        #self.bro.switch_to.frame('workFrame')


        # self.bro.switch_to.default_content()
        # # 回到主窗口
        # self.bro.switch_to.window(mainWindow)

    def getdata1(self,cxlist,chaxun):
        #回到workframe工作框架
        self.bro.switch_to.frame('workFrame')

        # 得到标题行项目
        bt = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt>td')
        # for btx in bt:
        #     print(btx.text)

        # "写入文件"
        with open(fr'data\{chaxun}{cxlist[-1]}.csv', 'w', encoding='utf-8') as f:
            for btx in bt:
                f.write(btx.text + ',')
            f.write('\n')
        # 读出修改
        with open(fr'data\{chaxun}{cxlist[-1]}.csv', 'r', encoding='utf-8') as f:
            # str=f.read()
            # print(str)
            lt = f.readlines()
            # print(lt[0])
            # print(type(lt[0]))
            lt[0] = lt[0].replace(', ,', ',')
            lt[0] = lt[0].replace('操作,', '查看,往期,')
            lt[0] = lt[0].replace(r'[201-1] 行业代码(GB/T4754-2017)', '行业代码')
            lt[0] = lt[0].replace('期别(年)', '期别年')
            lt[0] = lt[0].replace('期别(月)', '期别月')
            lt[0] = lt[0].replace(r';', '')
            lt[0] = lt[0].replace(r'1-本月', '本期')
            lt[0] = lt[0].replace('(损失以“-”号记)','')
            #lt[0] = lt[0].replace('本期资产处置收益(损失以“-”号记)千元', '本期资产处置收益千元')
            #lt[0] = lt[0].replace('上年同期资产处置收益(损失以“-”号记)千元', '上年同期资产处置收益千元')
            # lt[0] = lt[0].replace(r'1-本月;营业收入;千元', '本期营收')
            # lt[0] = lt[0].replace(r'上年同期;营业收入;千元', '上期营收')
            # print(lt[0])
            # print('#' * 30)
            # print(lt)
        # 再次写入修改好的标题行
        with open(fr'data\{chaxun}{cxlist[-1]}.csv', 'w', encoding='utf-8') as f:
            f.writelines(lt)

        # #先创建文件,如果以前有,则清空,写入一次，写入多次,操作一样！！！清空重写！！！可在外面询问是否重写...
        # with open(fr'data\{chaxun}{cxlist[-1]}.csv', 'w', encoding='utf-8') as f:
        #     f.write('')
        for cx in cxlist:
            select = Select(self.bro.find_element(By.NAME, "period"))

            # 通过 Select 对象选中
            select.select_by_visible_text(cx)
            time.sleep(1)
            # 30行数据
            datas = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt~tr')
            for data in datas:
                print(data.text)

            total_page = self.bro.find_element(By.ID, 'total_page')
            print(total_page.text)
            total_page = int(total_page.text[1:-1])
            # total_num=int(bro.find_element(By.ID,'total_num').text)
            # total_page=math.ceil(total_num/100)
            # print(total_page)
            page = 1
            for page in range(1, total_page + 1):
                # 退格符,两次,出现两次
                self.bro.find_element(By.XPATH,
                                      '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/input').send_keys(
                    '\b\b')
                self.bro.find_element(By.XPATH,
                                      '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/input').send_keys(page)
                self.bro.find_element(By.XPATH, '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[2]').click()
                trs = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt~tr')
                for tr in trs:
                    print(tr.text)
                #如果文件不存在，则创建，写入第一次后，追加写入
                #如果文件存在，则追加写入
                #if os.path.exists(fr'data\{chaxun}{cxlist[-1]}.csv'):
                with open(fr'data\{chaxun}{cxlist[-1]}.csv', 'a', encoding='utf-8') as f:
                    for tr in trs:
                        tds = tr.find_elements(By.TAG_NAME, 'td')
                        for td in tds:
                            f.write(td.text + ',')
                        f.write('\n')
                # else:
                #     with open(fr'data\{chaxun}{cxlist[-1]}.csv', 'w', encoding='utf-8') as f:
                #         f.write('')
        self.bro.switch_to.default_content()
        #self.closewindow()
        csvdictrw(fr'{chaxun}{cxlist[-1]}.csv')
        self.oldwindow()
        self.sql.run(fr'{chaxun}{cxlist[-1]}')

# bro=Mybro()
# bro.switchquery()
# #得到制度列表
# zdlist=bro.getzdopts()
# print(zdlist)
# #得到报表周期列表
# yjnlist=bro.getyjnopts()
# print(yjnlist)
# #得到报表列表
# #//*[@id="tables"]/tbody/tr[2]/td[4]/a,23----21年一套表制度,0---月报
# # bblist=bro.getbblist(zdlist[23],yjnlist[0])
# # print(bblist)
# # print(bblist[0][0])
# bro.getquery(zdlist[23],yjnlist[0])
# #XPATH对象,点击报表,5---财务报表
# # for bb in bblist:
# #     print(bb[0])
# #     bro.clickbb(bb[1])
# #     #得到查询列表
# #     cxmllist=bro.getquerylist()
# #     #print(cxmllist)
# #     cxmllist[0]=bb[0]
# #     print(cxmllist)
# #     bro.oldwindow()
# # lt=bro.getbiaoti(cxmllist[1])
# # print(lt)
# # bro.getdata()
# #bro.oldwindow()
# bro.closewindow()